<?php

namespace app\controller\Api\V1\Client;

use app\model\GameKitVersion;
use app\request\KitVersion;
use Exception;
use support\Request;
use support\Response;

class KitVersionController
{

    /**
     * 更新
     *
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function Download(Request $request): Response
    {

        $validate = new KitVersion;

        if (!$validate->scene('download')->check($request->all())) {
            throw new Exception($validate->getError(), 500);
        }

        if (!empty($request->input('version_number'))) {
            $info = GameKitVersion::query()->where('ulid', '>', $request->input('version_number'))
                ->latest('ulid')->first();
        } else {
            $info = GameKitVersion::query()->latest('ulid')->first();
        }

        if (!$info) {
            return jsonRe();
        } else {
            $info = $info->toArray();
            return jsonRe([
                'version_number' => $info['ulid'],
                'version_url' => $info['path'],
            ]);
        }

    }

}